41

Explore Your Deductive Logic—Sudoku

41

STEP 5

For i =​ 1 To 9

    For j =​ 1 To 9

      If sbox(i, j) =​ ““ Then

        whatisthisnumber =​ 0

        For k =​ 1 To 9

        If cantbelist(i, j, k) > 0 Then

          howmanycantbe =​ k

          whatisthisnumber =​ whatisthisnumber +​ cantbelist(i, j, k)

        End If

        Next k

          If howmanycantbe =​ 8 Then

            sbox(i, j) =​ 45 -​ whatisthisnumber

            Cells(i, j) =​ sbox(i, j)

            Call updatecantbelist(sbox(i, j), i, j)

          End If

        End If

      Next j

Next i

3.2  ITERATIVE BUILD OF CANTBELIST

Now let’s talk about iterations. As soon as you find a missing cell, there is a need

to update the cantbelist. This is why we have the call to updatecantbelist—​which is

made up of the same steps as steps 2 to 4. (Full code is in the back of this book.) But

if cantbelist has changed, you need to reexamine cantbelist because now there may be

more cells reaching the threshold of eight “cantbelist” entries. This is why we need

to go through this code again and again. You can control how many times you want

to repeat. In the simplest way, you can just repeat it ten times. In order to repeat ten

times, you will need extra lines of code in step 5, shown below in red. Note the use of

the “inputbox” statement. This allows us to ask the user if the user wants to go ahead

with the next iteration. It will perform the next iteration only if the user answers “Y”

for yes. This will allow the user to get a few numbers at a time, but not all of them, so

that the pleasure of solving the puzzle can be retained a bit longer.

STEP 5

For Iteration =​ 1 To 10

For i =​ 1 To 9

For j =​ 1 To 9

    If sbox(i, j) =​ ““ Then

      whatisthisnumber =​ 0

      For k =​ 1 To 9

      If cantbelist(i, j, k) > 0 Then

        howmanycantbe =​ k

        whatisthisnumber =​ whatisthisnumber +​ cantbelist(i, j, k)

      End If